/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.db;

import java.io.Serializable;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

public class Db {

	// --------------------------------------------------------
	// use SqlSession
	// --------------------------------------------------------
	public static <T> DbPro<T> use(SqlSession sqlSession, Class<T> entityClass) {
		return DbPro.use(sqlSession, entityClass);
	}

	// --------------------------------------------------------
	// use Config
	// --------------------------------------------------------
	public static <T> DbPro<T> use(Class<T> entityClass) {
		return use(DbKit.MAIN_CONFIG_NAME, entityClass);
	}

	public static <T> DbPro<T> use(String configName, Class<T> entityClass) {
		return DbPro.use(configName, entityClass);
	}

	// --------------------------------------------------------
	// select
	// --------------------------------------------------------
	public static <T> T selectByPrimaryKey(Class<T> entityClass, @Param("id") Serializable idVal) {
		return use(entityClass).selectByPrimaryKey(idVal);
	}

	public static <T> T selectByPrimaryKeys(Class<T> entityClass, T record) {
		return use(entityClass).selectByPrimaryKeys(record);
	}

	public static <T> T selectOneByWhereSql(Class<T> entityClass, @Param("sql") String sql, @Param("param") Object param) {
		return use(entityClass).selectOneByWhereSql(sql, param);
	}

	public static <T> List<T> selectListByWhereSql(Class<T> entityClass, @Param("sql") String sql, @Param("param") Object param) {
		return use(entityClass).selectListByWhereSql(sql, param);
	}

	public static <T> List<T> selectList(Class<T> entityClass, T record) {
		return use(entityClass).selectList(record);
	}

	public static <T> T selectOne(Class<T> entityClass, T record) {
		return use(entityClass).selectOne(record);
	}

	public static <T> int selectCount(Class<T> entityClass) {
		return use(entityClass).selectCount();
	}

	public static <T> int selectCountByWhereSql(Class<T> entityClass, @Param("sql") String sql, @Param("param") Object param) {
		return use(entityClass).selectCountByWhereSql(sql, param);
	}

	// --------------------------------------------------------
	// update
	// --------------------------------------------------------
	public static <T> int updateByPrimaryKey(Class<T> entityClass, T record) {
		return use(entityClass).updateByPrimaryKey(record);
	}

	public static <T> int updateByPrimaryKeySelective(Class<T> entityClass, T record) {
		return use(entityClass).updateByPrimaryKeySelective(record);
	}

	public static <T> int updateByWhereSql(Class<T> entityClass, @Param("record") T record, @Param("sql") String sql,
			@Param("param") Object param) {
		return use(entityClass).updateByWhereSql(record, sql, param);
	}

	public static <T> int updateByWhereSqlSelective(Class<T> entityClass, @Param("record") T record, @Param("sql") String sql,
			@Param("param") Object param) {
		return use(entityClass).updateByWhereSqlSelective(record, sql, param);
	}

	// --------------------------------------------------------
	// insert
	// --------------------------------------------------------
	public static <T> int insertSelective(Class<T> entityClass, T record) {
		return use(entityClass).insertSelective(record);
	}

	public static <T> int insert(Class<T> entityClass, T record) {
		return use(entityClass).insert(record);
	}

	// --------------------------------------------------------
	// delete
	// --------------------------------------------------------
	public static <T> int delete(Class<T> entityClass, T record) {
		return use(entityClass).delete(record);
	}

	public static <T> int deleteByPrimaryKeys(Class<T> entityClass, T record) {
		return use(entityClass).deleteByPrimaryKeys(record);
	}

	public static <T> int deleteByPrimaryKey(Class<T> entityClass, @Param("id") Serializable key) {
		return use(entityClass).deleteByPrimaryKey(key);
	}

	public static <T> int deleteByWhereSql(Class<T> entityClass, @Param("sql") String sql, @Param("param") Object param) {
		return use(entityClass).deleteByWhereSql(sql, param);
	}
}
